package com.hongkzh.www.shop.controller;

import com.hongkzh.www.conmon.ReqResponse;
import com.hongkzh.www.shop.service.CategoryService;
import com.hongkzh.www.shop.service.ProductService;
import com.hongkzh.www.swagger.annotation.*;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author martins
 */
@Api(tag = "shop", description = "乐购")
public class ShopController extends Controller {
    CategoryService categoryService=new CategoryService();
    ProductService productService=new ProductService();

    @ApiOperation(url = "/v1/shop/getCategoryList", tag = "shop", httpMethod = "post",description = "乐购一级分类")

    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{    categoryId  分类id    name 名称   }"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void getCategoryList(){
        ReqResponse<List<Record>> r=new ReqResponse();
        List<Record>  record= categoryService.getCategoryList();
        r.setData(record);
        renderJson(r);
    }
    @ApiOperation(url = "/v1/shop/getSubCategoryList", tag = "shop", httpMethod = "post",description = "乐购子分类")
    @Params({
            @Param(name = "categoryId", description = "分类id ", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{ imgSrc 上部广告图   categorys[ categoryId  分类id  imgSrc 图片 name 名称 ] }"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void getSubCategoryList(){
        String pid=getPara("categoryId");
        ReqResponse<Record> r=new ReqResponse();
        List<Record>  records= categoryService.getSubCategoryList(pid);
        Record p=categoryService.getCategoryById(pid);
        Record record=new Record();
        record.set("imgSrc",p.get("imgSrc"));
        record.set("categorys",records);
        r.setData(record);
        renderJson(r);
    }
    @ApiOperation(url = "/v1/shop/getCategoryProductList", tag = "shop", httpMethod = "post",description = "乐购子分类商品")
    @Params({
            @Param(name = "categoryId", description = "分类id ", required = true, dataType = "string"),
            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string"),
            @Param(name = "sortId", description = "排序字段 综合create_date  价格 integral ", required = false, dataType = "string"),
            @Param(name = "sortValue", description = "排序值 asc 升 desc 降 ", required = false, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{    categorys[  productId 商品id imgSrc 图片 title 标题 subtitle 副标题 integral 价格 ] }"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void getCategoryProductList(){
        String pid=getPara("categoryId");
        int pageNumber=getParaToInt("pageNumber");
        String sortId=getPara("sortId");
        String sortValue=getPara("sortValue");
        if(StrKit.isBlank(sortId)){
            sortId="create_date";
        }
        if(StrKit.isBlank(sortValue)){
            sortValue="desc";
        }
        ReqResponse<Page<Record>> r=new ReqResponse();
        Page<Record>  records= productService.getCategoryProductList(pid,pageNumber,sortId,sortValue);

        r.setData(records);
        renderJson(r);
    }
    @ApiOperation(url = "/v1/shop/getMainList", tag = "shop", httpMethod = "post",description = "乐购主页接口")

    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{ carousels 轮播图[imgSrc图片路径 imgRank 序号   model 1商品2专题  productId 产品id specialId 专题id] " +
                            "luckyBursts 爆款活动[productId 产品id imgSrc图片 integral价格  title 标题 ，activityStocks 活动库存 activityPrice活动价格 percentage剩余百分比  ]  " +
                            "specials 专题活动[imgSrc图片 title标题 subtitle副标题 specialId 专题id]  "+
                            "seckills 秒杀活动[productId 产品id imgSrc图片 integral价格   title 标题 ， subtitle 副标题 activityStocks 活动库存 activityPrice活动价格 percentage剩余百分比 currentTime 当前时间 beginDate 开始时间 endDate 结束时间 sortDate 离现在时间]  "+
                            "discounts 折扣活动[productId 产品id imgSrc图片 integral价格   title 标题 ，activityStocks 活动库存 activityPrice活动价格 discount折扣 people 参与人数]  "+
                            "categorys 分类活动{categoryId分类id  categoryName 分类名称 imgSrc分类图片 [products { productId 产品id imgSrc图片   title 标题 subtitle 副标题}]}  "+
                            "}"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void getMainList(){

        ReqResponse<Record> r=new ReqResponse();
        List<Record>  carousels= categoryService.getCarouselList(1);
        List<Record>  specials= productService.getSpecialsList();
        List<Record>  luckyBursts= productService.getLuckyBurstsList();
        List<Record>  seckills= productService.getSeckillsList();
        List<Record>  discounts= productService.getdDiscountsList();

        List<Record>  categorys= productService.getCategorysList();

        Record record=new Record();
        record.set("specials",specials);
        record.set("carousels",carousels);
        record.set("luckyBursts",luckyBursts);
        record.set("seckills",seckills);
        record.set("discounts",discounts);
        record.set("categorys",categorys);
        r.setData(record);
        renderJson(r);
    }
    @ApiOperation(url = "/v1/shop/getNewMainList", tag = "shop", httpMethod = "post",description = "乐购新品主页接口")

    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{ carousels 轮播图[imgSrc图片路径 imgRank 序号   model 1商品2专题  productId 产品id specialId 专题id] " +
                            "recommends 推荐[productId 产品id imgSrc图片 integral价格  title 标题 ，subtitle 副标题   ]  " +
                            "}"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void getNewMainList(){

        ReqResponse<Record> r=new ReqResponse();
        List<Record>  carousels= categoryService.getCarouselList(2);
        List<Record>  recommends= productService.getNewRecommendsList();


        Record record=new Record();
        record.set("recommends",recommends);
        record.set("carousels",carousels);

        r.setData(record);
        renderJson(r);
    }
    @ApiOperation(url = "/v1/shop/getNewProductList", tag = "shop", httpMethod = "post",description = "乐购新品商品列表")
    @Params({

            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string"),
            @Param(name = "sortId", description = "排序字段 综合create_date  价格 integral ", required = false, dataType = "string"),
            @Param(name = "sortValue", description = "排序值 asc 升 desc 降 ", required = false, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{      productId 商品id imgSrc 图片 title 标题 subtitle 副标题 integral 价格  }"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void getNewProductList(){
        int pageNumber=getParaToInt("pageNumber");
        String sortId=getPara("sortId");
        String sortValue=getPara("sortValue");
        if(StrKit.isBlank(sortId)){
            sortId="create_date";
        }
        if(StrKit.isBlank(sortValue)){
            sortValue="desc";
        }
        ReqResponse<Page<Record>> r=new ReqResponse();
        Page<Record>  records= productService.getNewProductList(pageNumber,sortId,sortValue);

        r.setData(records);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/shop/getDiscountsList", tag = "shop", httpMethod = "post",description = "乐购折扣列表")
    @Params({

            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{ beginDate 开始时间 endDate 结束时间 sortDate 离结束多久  currentTime 当前时间  records {      list[ productId 产品id imgSrc图片 integral价格   title 标题 ，activityStocks 活动库存 activityPrice活动价格 discount折扣 people 参与人数 ] }}"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void getDiscountsList(){
        int pageNumber=getParaToInt("pageNumber");

        ReqResponse<Map> r=new ReqResponse();
        Page<Record>  records= productService.getdDiscountsList(pageNumber);
        Map map=new HashMap();
        map.put("records",records);
        int currentHour=0,beginDate=0,endDate=0 ;
        String currentTime="";
        if(null!=records&&null!=records.getList()&&records.getList().size()>0) {
            currentHour = records.getList().get(0).getInt("currentHour");
            beginDate =  records.getList().get(0).getInt("beginDate");
            endDate =  records.getList().get(0).getInt("endDate");
            currentTime= records.getList().get(0).getStr("currentTime");
        }
        map.put("currentTime", currentTime);
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        map.put("currentHour", currentHour);
        map.put("sortDate", currentHour-beginDate);
        if(currentHour>=beginDate&&currentHour<endDate){
            map.put("sortDate",0);
        }
        else{
            if(currentHour-beginDate>0) {
                map.put("sortDate", currentHour-beginDate-24);
            }
        }
        r.setData(map);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/shop/getLuckyBurstALLList", tag = "shop", httpMethod = "post",description = "乐购爆款列表")
    @Params({

            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{ beginDate 开始时间 endDate 结束时间 sortDate 离结束多久  currentTime 当前时间  records {      list[ productId 产品id imgSrc图片 integral价格  subtitle 副标题 title 标题 ，activityStocks 活动库存 activityPrice活动价格 lastStocks 剩余库存 [users {  uid 用户id,headImg头像 ,uName 用户名称} ] ] }}"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void getLuckyBurstALLList(){
        int pageNumber=getParaToInt("pageNumber");

        ReqResponse<Map> r=new ReqResponse();
        Page<Record>  records= productService.getLuckyBurstALLList(pageNumber);
        Map map=new HashMap();
        map.put("records",records);
        int currentHour=0,beginDate=0,endDate=0 ;
        String currentTime="";
        if(null!=records&&null!=records.getList()&&records.getList().size()>0) {
            currentHour = records.getList().get(0).getInt("currentHour");
            beginDate =  records.getList().get(0).getInt("beginDate");
            endDate =  records.getList().get(0).getInt("endDate");
            currentTime= records.getList().get(0).getStr("currentTime");
        }
        map.put("currentTime", currentTime);
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        map.put("currentHour", currentHour);
        map.put("sortDate", currentHour-beginDate);
        if(currentHour>=beginDate&&currentHour<endDate){
            map.put("sortDate",0);
        }
        else{
            if(currentHour-beginDate>0) {
                map.put("sortDate", currentHour-beginDate-24);
            }
        }
        r.setData(map);
        renderJson(r);
    }



    @ApiOperation(url = "/v1/shop/getSeckillALLList", tag = "shop", httpMethod = "post",description = "乐购秒杀列表")
    @Params({

            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{     beginDate 开始时间 endDate 结束时间 sortDate 离结束多久  currentTime 当前时间    productId 产品id imgSrc图片 integral价格   subtitle 副标题  title 标题 ，activityStocks 活动库存 activityPrice活动价格 lastStocks 剩余库存 [users {  uid 用户id,headImg头像 ,uName 用户名称} ]   }"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void getSeckillALLList(){
        int pageNumber=getParaToInt("pageNumber");

        ReqResponse<Page<Record>> r=new ReqResponse();
        Page<Record>  records= productService.getSeckillALLList(pageNumber);


        r.setData(records);
        renderJson(r);
    }


    @ApiOperation(url = "/v1/shop/getProductById", tag = "shop", httpMethod = "post",description = "乐购商品详情")
    @Params({

            @Param(name = "productId", description = "产品id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{ productId 产品id  title 标题 subtitle 副标题 integral 价格 afterService 服务 descript 详情    images [imgSrc] 主轮播图 colors [productColorId 产品颜色id name 颜色名称] specs [productSpecId 产品规格id name 规格名称] skus [productSpecId 产品规格id productColorId 产品颜色id  sukId sku的id stocks 库存 integral 价格 ] " +
                            "recs 推荐[productId 产品id  title 标题  integral 价格   imgSrc 主 图]" +
                            "comment评论 {{  userId 评论用户 uName 评论用户名称 headImg 评论用户头像 content 内容 replyUserId 回复id rName回复用户名称 rheadImg回复用户头像 createDate 创建时间  images[imgSrc 评论图片] } } }"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void getProductById(){
        String productId=getPara("productId");

        ReqResponse<Record> r=new ReqResponse();
        Record record=productService.getProductById(productId);
        r.setData(record);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/shop/getProductCommentById", tag = "shop", httpMethod = "post",description = "乐购商品评价")
    @Params({

            @Param(name = "productId", description = "产品id", required = true, dataType = "string"),
            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{ list[{ replyContent 回复内容 orderDate 订单日期 specName 规格名称 colorName颜色名称 star 评分等级 userId 评论用户 uName 评论用户名称 headImg 评论用户头像 content 内容 replyUserId 回复id rName回复用户名称 rheadImg回复用户头像 createDate 创建时间  images[imgSrc 评论图片] }    }"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void getProductCommentById(){
        String productId=getPara("productId");
        int pageNumber=getParaToInt("pageNumber");
        ReqResponse<Page<Record>> r=new ReqResponse();
        Page<Record> record=productService.getProductCommentById(productId,pageNumber);
        r.setData(record);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/shop/getProductSkuBySku", tag = "shop", httpMethod = "post",description = "乐购商品Sku信息")
    @Params({

            @Param(name = "skuId", description = "产品id", required = true, dataType = "string")

    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "{ productId 产品id integral 价格 skuId  stocks 库存  images[{imgSrc 图片   第一张为主图  }]  }"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void getProductSkuBySku(){
        String skuId=getPara("skuId");

        ReqResponse<Record> r=new ReqResponse();
        Record record= productService.getProductSkuBySku(skuId);
        List<Record> records=productService.getProductSkuImagesBySku(skuId);
        record.set("images",records);
        r.setData(record);
        renderJson(r);
    }
}

